package com.rxjava;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.BehaviorSubject;

public class BehaviorSubjectDemo {

	public static void main(String[] args) {
		//Observer会接收到BehaviorSubject被订阅之前的最后一个数据，再接收其他发射过来的数据，如果BehaviorSubject被订阅之前没有发送任何数据，
		//则会发送一个默认数据。（注意跟AsyncSubject的区别，AsyncSubject要手动调用onCompleted()，且它的Observer会接收到onCompleted()前发送的最后一个数据，
		//之后不会再接收数据，而BehaviorSubject不需手动调用onCompleted()，它的Observer接收的是BehaviorSubject被订阅前发送的最后一个数据，两个的分界点不一样，且之后还会继续接收数据。）
		BehaviorSubject<String> behaviorSubject = BehaviorSubject.createDefault("default");
		behaviorSubject.onNext("behaviorSubject1");
		behaviorSubject.onNext("behaviorSubject2");
		behaviorSubject.subscribe(new Observer<String>() {
			@Override
			public void onComplete() {
				System.out.println("behaviorSubject:complete");
			}

			@Override
			public void onError(Throwable e) {
				System.out.println("behaviorSubject:error");
			}

			@Override
			public void onNext(String s) {
				System.out.println("behaviorSubject:" + s);
			}

			@Override
			public void onSubscribe(Disposable disposable) {

			}
		});

		behaviorSubject.onNext("behaviorSubject3");
		behaviorSubject.onNext("behaviorSubject4");
	}

}
